/**
 * 
 */
package com.ls.lsqc.modules.sys.entity;

import java.util.List;

import javax.validation.constraints.NotNull;

import org.hibernate.validator.constraints.Length;

import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.ls.lsqc.common.persistence.DataEntity;

/**
 * 菜单Entity
 * @author 
 * @version 2013-05-15
 */
public class Menu extends DataEntity<Menu> {

	private static final long serialVersionUID = 1L;
	private Menu parent;	// 父级菜单
	private String parentIds; // 所有父级编号
	private String menuName; 	// 名称
	private String menuHref; 	// 链接
	private String menuTarget; 	// 目标（ mainFrame、_blank、_self、_parent、_top）
	private String menuIcon; 	// 图标
	private Integer menuSort; 	// 排序
	private String menuIsShow; 	// 是否在菜单中显示（1：显示；0：不显示）
	private String menuPermission; // 权限标识
	
	private String menuNotShow;  //不显示的菜单
	
	private Long userId;
	
	public Menu(){
		super();
		this.menuSort = 30;
		this.menuIsShow = "1";
	}
	
	public Menu(Long id){
		super(id);
	}
	
	@JsonBackReference
	@NotNull
	public Menu getParent() {
		return parent;
	}

	public void setParent(Menu parent) {
		this.parent = parent;
	}

	@Length(min=1, max=2000)
	public String getParentIds() {
		return parentIds;
	}

	public void setParentIds(String parentIds) {
		this.parentIds = parentIds;
	}
	
	@Length(min=1, max=100)
    public String getMenuName() {
        return menuName;
    }

    public void setMenuName(String menuName) {
        this.menuName = menuName;
    }

    @Length(min=0, max=2000)
    public String getMenuHref() {
        return menuHref;
    }

    public void setMenuHref(String menuHref) {
        this.menuHref = menuHref;
    }

	@Length(min=0, max=20)
	   public String getMenuTarget() {
        return menuTarget;
    }

    public void setMenuTarget(String menuTarget) {
        this.menuTarget = menuTarget;
    }
	
	@Length(min=0, max=100)
	public String getMenuIcon() {
        return menuIcon;
    }

    public void setMenuIcon(String menuIcon) {
        this.menuIcon = menuIcon;
    }

    @NotNull
	public Integer getMenuSort() {
        return menuSort;
    }

    public void setMenuSort(Integer menuSort) {
        this.menuSort = menuSort;
    }

    @Length(min=1, max=1)
    public String getMenuIsShow() {
        return menuIsShow;
    }

    public void setMenuIsShow(String menuIsShow) {
        this.menuIsShow = menuIsShow;
    }
	@Length(min=0, max=200)
    public String getMenuPermission() {
        return menuPermission;
    }

    public void setMenuPermission(String menuPermission) {
        this.menuPermission = menuPermission;
    }


	public Long getParentId() {
		return parent != null && parent.getId() != null ? parent.getId() : 0L;
	}

    public String getMenuNotShow() {
        return menuNotShow;
    }

    public void setMenuNotShow(String menuNotShow) {
        this.menuNotShow = menuNotShow;
    }

    @JsonIgnore
	public static void sortList(List<Menu> list, List<Menu> sourcelist, Long parentId, boolean cascade){
		for (int i=0; i<sourcelist.size(); i++){
			Menu e = sourcelist.get(i);
			System.out.println(e.getParent().getId()+"================"+parentId);
			if (e.getParent()!=null && e.getParent().getId()!=null
					&& e.getParent().getId()==parentId){
			  
				list.add(e);
				if (cascade){
					// 判断是否还有子节点, 有则继续获取子节点
					for (int j=0; j<sourcelist.size(); j++){
						Menu child = sourcelist.get(j);
						if (child.getParent()!=null && child.getParent().getId()!=null
								&& child.getParent().getId().equals(e.getId())){
							sortList(list, sourcelist, e.getId(), true);
							break;
						}
					}
				}
			}
		}
	}

	@JsonIgnore
	public static Long getRootId(){
		return 1L;
	}
	
	public Long getUserId() {
		return userId;
	}

	public void setUserId(Long userId) {
		this.userId = userId;
	}

	@Override
	public String toString() {
		return menuName;
	}
}